#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: shixin_company_list.py
@time: 2018/2/4 20:46
"""

import sys

sys.path.append('../..')
from common import util
from common.mongo import MongDb
from logger import Logger

COMPANY_MONGO_DB = {
    'host': '172.16.215.2',
    'port': 40042,
    'db': 'company_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('shixin_ningbo_company_list.log').get_logger()

source_db = MongDb(COMPANY_MONGO_DB['host'], COMPANY_MONGO_DB['port'], COMPANY_MONGO_DB['db'],
                   COMPANY_MONGO_DB['username'],
                   COMPANY_MONGO_DB['password'], log=log)


def main():
    source_table = 'zhejiang_shixin_company_list'
    target_table = 'ningbo_shixin_company_list'
    search_table = 'new_zhejiang_search_list'
    count = 0
    total = 0
    result_list = []
    for item in source_db.traverse_batch_field(source_table, {}, ['_id']):
        total += 1
        _id = item.get('_id')
        result = source_db.find_one(search_table, {'_id': _id})
        if result is None:
            continue

        register_code = result.get('register_code')
        if not isinstance(register_code, basestring):
            continue

        if '3302' in register_code or u'3302' in register_code:
            result_list.append({
                '_id': _id,
                'register_code': register_code,
                '_in_time': util.get_now_time()
            })
            count += 1

        if len(result_list) >= 1000:
            source_db.insert_batch_data(target_table, result_list)
            del result_list[:]

        if count % 1000 == 0:
            log.info("当前进度: total = {} count = {}".format(total, count))

    if len(result_list) > 0:
        source_db.insert_batch_data(target_table, result_list)
        del result_list[:]
    log.info("查找完成: total = {} count = {}".format(total, count))


if __name__ == '__main__':
    main()
